﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QCM.Framework
{
    public partial class QuestionCtrl : UserControl
    {
        #region Gestion des propritetes et de leurs evenements
        public event OrientationEventHandler OrientationTitleChanged;
        public void OnOrientationTitleChanged()
        {
            if (OrientationTitleChanged != null)
            {
                OrientationTitleChanged(this, 
                    new OrientationEventArgs(this.OrientationTitle));
            }
        }
        Orientation orientationTitle;
        public Orientation OrientationTitle
        {
            get { return this.orientationTitle; }
            set
            {
                if (this.orientationTitle != value)
                {
                    this.orientationTitle = value;
                    if (this.OrientationTitle == Orientation.Horizontal)
                    {
                        int height = this.panelTitle.Height;
                        this.panelTitle.Height = this.panelTitle.Width;
                        this.panelTitle.Width = height;
                        this.panelTitle.Dock = DockStyle.Top;
                    }
                    else
                    {
                        int width = this.panelTitle.Width;
                        this.panelTitle.Width = this.panelTitle.Height;
                        this.panelTitle.Height = width;
                        this.panelTitle.Dock = DockStyle.Left;
                    }
                    this.panelTitle.Invalidate();
                    OnOrientationTitleChanged();
                }

            }
        }

        public class Question
        {
            public string Titre;
            public int Id;
            public List<Reponse> Reponses;

            public Question(string titre, int id)
            {
                this.Titre = titre;
                this.Id = id;
                this.Reponses = new List<Reponse>();
            }
        }
        public class Reponse
        {
            public string Text;
            public int Valeur;

            public Reponse(string text, int valeur)
            {
                this.Text = text;
                this.Valeur = valeur;
            }
        }
        Question laQuestion;
        public Question LaQuestion
        {
            get { return this.laQuestion; }
            set
            {
                this.laQuestion = value;
                InitializePerso();
            }
        }

        float sizeInterligneTitle;
        System.Drawing.Font fontTitle;
        public System.Drawing.Font FontTitle
        {
            get { return this.fontTitle; }
            set
            {
                if (this.fontTitle != value)
                {
                    this.fontTitle = value;
                    this.sizeInterligneTitle = 
                        this.fontTitle.GetHeight() / 2;
                    this.panelTitle.Size = 
                        new Size((int)this.fontTitle.GetHeight() * 2,
                            (int)this.fontTitle.GetHeight() * 2);
                    this.panelTitle.Invalidate();
                }
            }
        }
        public Color BackColorTitle
        {
            get { return this.panelTitle.BackColor; }
            set { this.panelTitle.BackColor = value; }
        }
        
        System.Drawing.Font fontQuestion;
        public System.Drawing.Font FontQuestion
        {
            get { return this.fontQuestion; }
            set
            {
                if (this.fontQuestion != value)
                {
                    this.fontQuestion = value;
                    this.panelQuestion.Invalidate();
                }
            }
        }
        public Color BackColorQuestion
        {
            get { return this.panelQuestion.BackColor; }
            set { this.panelQuestion.BackColor = value; }
        }

        public new Size Size
        {
            get { return base.Size; }
            private set { base.Size = value; } 
        }

        #endregion

        public QuestionCtrl()
        {
            InitializeComponent();
            this.fontTitle = this.Font;
        }

        private List<System.Windows.Forms.CheckBox> reponses;

        private void InitializePerso()
        {
            if (this.laQuestion != null)
                if (this.laQuestion.Reponses != null)
                {
                        
                    this.reponses = new List<CheckBox>(laQuestion.Reponses.Count);
                    foreach (Reponse rep in laQuestion.Reponses)
                    {

                        System.Windows.Forms.CheckBox checkBox = new System.Windows.Forms.CheckBox();
                        this.panelReponses.Controls.Add(checkBox);
                        checkBox.Size = new Size(180, 17);
                        checkBox.Dock = DockStyle.Bottom;
                        checkBox.Name = rep.Text;   
                        checkBox.Text = rep.Text;
                        checkBox.UseVisualStyleBackColor = true;
                    }
                    ResizeQuestionCtrl();
                }
        }

        private void ResizeQuestionCtrl()
        {
            Size ctrl = this.Size;
            if (OrientationTitle == Orientation.Horizontal)
            {
                ctrl.Height = this.panelTitle.Height + this.panelQuestion.Height;
            }
            else
            {
                ctrl.Height = this.panelQuestion.Height + (laQuestion == null ? 0 : (laQuestion.Reponses.Count * 17));
            }
            this.Size = ctrl;
        }

        private void panelTitle_Paint(object sender, PaintEventArgs e)
        {
            if (this.laQuestion != null)
            {
                System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Point(10, 10), new Point(120, 10), Color.Black, Color.Ivory);
                if (this.orientationTitle == Orientation.Horizontal)
                {
                    e.Graphics.DrawString(laQuestion.Titre, this.FontTitle, brush, new PointF(sizeInterligneTitle, sizeInterligneTitle));
                }
                else
                {
                    e.Graphics.TranslateTransform(0, Height);
                    e.Graphics.RotateTransform(-90.0F);
                    e.Graphics.DrawString(laQuestion.Titre, this.FontTitle, brush, new PointF(sizeInterligneTitle, sizeInterligneTitle));
                }
            }
        }

        private void panel_SizeChanged(object sender, EventArgs e)
        {
            ResizeQuestionCtrl();
        }

        private void panelTitle_Click(object sender, EventArgs e)
        {
            if (OrientationTitle == Orientation.Horizontal)
                this.OrientationTitle = Orientation.Vertical;
            else
                this.OrientationTitle = Orientation.Horizontal;
        }
    }
}
